<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/9/009
 * Time: 10:57
 */

namespace app\api\model;


class FranchiseArea extends BaseModel {
    /**
     * 地址对应的特许店铺
     * @return \think\model\relation\belongsTo
     */
    public function franchise() {
        return $this->belongsTo('Franchise');
    }

    public static function FranchiseList($address) {
        $franchise = self::alias('fa')->field('f.name, f.industry, f.id')
            ->join('yybs_franchise f', 'f.id = fa.franchise_id')
            ->where('(level = 1 AND province = '.$address[0].') OR (level = 2 AND city = '.$address[1].') OR (level = 3 AND area = '.$address[2].')  OR level = 4 ')
            ->where('f.status', 1)->where('f.delete_time', 0)
            ->order('f.sort desc, f.id desc')
            ->paginate(15, true);


        return $franchise;
//            echo model('franchise_area')->getLastSql();
    }

    public static function FranchiseCount($address) {
        $count = self::alias('fa')->field('f.name, f.industry, f.id')
            ->join('yybs_franchise f', 'f.id = fa.franchise_id')
            ->where('(level = 1 AND province = '.$address[0].') OR (level = 2 AND city = '.$address[1].') OR (level = 3 AND area = '.$address[2].')  OR level = 4  ')
            ->where('f.status', 1)->where('f.delete_time', 0)
            ->count();

        return $count;
    }
}